/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_02_oop.inner_classes.Example_01_Anonymous_Inner_Class.java]
 * Creation Date 	[21-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_oop.inner_classes;

/**
 * 匿名内部类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_02_Anonymous_Inner_Class {

	private Popcorn popcorn = new Popcorn() {
		@Override
		public void pop() {
			System.out.println("匿名内部类形式1");
		}

		@Override
		public String toString() {
			this.pop();
			return "";
		}
	};

	/**
	 * 测试匿名内部类
	 * 
	 * @param popcorn
	 *            传入的匿名内部类对象
	 */
	public void popMethod(Popcorn popcorn) {
		System.out.println("匿名内部类形式2");
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Example_02_Anonymous_Inner_Class anonymousInnerClass = new Example_02_Anonymous_Inner_Class();
		System.out.println(anonymousInnerClass.popcorn);
		anonymousInnerClass.popMethod(new Popcorn() {
			@Override
			public void pop() {
				System.out.println("La La La");
			}
		});

		// 一种比较好的语法, 类似于匿名内部类
		// ArrayList<String> friends = new ArrayList<>();
		// favorites.add("Harry");
		// favorites.add("Tony");
		// invite(friends);
		// 等价于
		// invite(new ArrayList<String>() {{ add("Harry"); add("Tony"); }})

	}

}

/**
 * 演示用的接口
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
interface Popcorn {

	/**
	 * pop method
	 */
	public void pop();
}